<?php

namespace app\admin\controller;

use app\common\controller\Backend;

class Comment extends Backend
{
    public $navTitle = 'comment';

    public function __construct()
    {
        parent::__construct();

        $this->CommentModel = model('Post.Comment');
    }

    public function index()
    {
        // TODO 查询评论数据
        $commentList = $this->CommentModel->with(['user', 'post'])->paginate(10);

        if ($this->request->isGet()) {
            $search_field = $this->request->param('search_field', '');
            $keyword = $this->request->param('keyword', '');

            if ($keyword == '') $commentList = $this->CommentModel->with(['user', 'post'])->paginate(10);

            if ($search_field == 'userid') {
                $user = model('User.User')->where('nickname', 'like', "%{$keyword}%")->find();
                // halt($keyword);

                // halt($user['id'].'-'.$user['nickname']);
                $commentList = $this->CommentModel
                    ->with(['user', 'post'])
                    ->where('comment.userid', 'like', "%{$user['id']}%")
                    ->paginate(10);
            }

            // if ($search_field == 'postid') {
            //     $post = model('Post.Post')->where('title', 'like', '%{$keyword}%')->find();

            //     $commentList = $this->CommentModel
            //         ->with(['user', 'post'])
            //         ->where('comment.postid', 'like', "%{$post['id']}%")
            //         ->paginate(10);
            // }
        }

        return $this->fetch('', [
            'commentList' => $commentList,
            'navTitle' => $this->navTitle,
        ]);
    }

    public function delete()
    {
        if ($this->request->isAjax()) {
            // TODO 接收action
            $action = $this->request->param('action', '');

            // TODO 接收全部参数
            $formData = $this->request->param();

            if ($action == 'del') {
                $comid = $formData['comid'] ? $formData['comid'] : '';

                // TODO 查询comment
                $comment = $this->CommentModel->field('id,pid,postid')->find($comid);

                if (!$comment) $this->error('评论不存在');

                // TODO 查询帖子所有评论
                $commentList = $this->CommentModel
                    ->field('id,pid')
                    ->where(['postid' => $comment['postid']])
                    ->select();

                $resCommentList = CommentDelete($commentList, $comid);

                $resCommentList[] = (int)$comid;

                // halt($resCommentList);

                $result = $this->CommentModel->destroy($resCommentList);

                if ($result === FALSE) $this->error('删除评论失败,' . $this->CommentModel->getError());

                $this->success('删除成功', url('admin/comment/list'));
            }

            if ($action == 'dels') {
                // TODO 转存idArr
                $idArr = $formData['idArr'] ? $formData['idArr'] : [];

                $result = $this->CommentModel->destroy($idArr);

                if ($result === FALSE) $this->error('删除失败,' . $this->CommentModel->getError());

                $this->success('删除成功', url('admin/comment/list'));
            }
        }
    }

    public function info()
    {
        if ($this->request->isAjax()) {
            $comid = $this->request->param('comid', '');

            $comment = $this->CommentModel->with('user')->find($comid);

            if (!$comment) $this->error('评论不存在');

            $this->success('查询成功', null, $comment);
        }
    }

    // ? 回收站
    public function appman()
    {
        if ($this->request->isAjax()) {
            $action = $this->request->param('action', '');

            // TODO 接收数据
            $formData = $this->request->param();

            // TODO 还原
            if ($action == 'returns') {
                // TODO 转存idArr
                $idArr = $formData['idArr'] ? $formData['idArr'] : [];

                // 拿id
                $result = $this->CommentModel->onlyTrashed()->whereIn('id', $idArr)->update(['deletetime' => null]);

                // UPDATE `pre_admin` INTO `deletetime` = null WHERE `id` IN 1,2,3

                // TODO 这注意三元表达式如果变量值为false 也就是还原时失败，执行前面的语句
                $result === FALSE ?  $this->error('还原失败，' . $this->CommentModel->getError()) : $this->success('还原成功');
            }

            if ($action == 'return') {
                $comid = $this->request->param('comid', '');

                $comment = $this->CommentModel->onlyTrashed()->find($comid);

                if (!$comment) $this->error('评论不存在');

                $result = $comment->restore();

                if ($result === FALSE) $this->error('还原失败,' . model('Post.Comment')->getError());

                $this->success('还原成功', url('admin/comment/index'));
            }

            // TODO 删除
            if ($action == 'del') {
                $comid = $this->request->param('comid', '');

                $comment = $this->CommentModel->onlyTrashed()->find($comid);

                if (!$comment) $this->error('评论不存在');

                $result = $this->CommentModel->destroy($comid, true);

                if ($result === FALSE) $this->error('删除失败');

                $this->success('删除成功', url('admin/comment/index'));
            }

            if ($action == 'dels') {
                // TODO 转存idArr
                $idArr = $formData['idArr'];

                $result = $this->CommentModel->destroy($idArr, true);

                if ($result === FALSE) $this->error('删除失败');

                $this->success('删除成功', url('admin/comment/index'));
            }
        }

        $commentList = $this->CommentModel->onlyTrashed('deletetime')->order('deletetime', 'desc')->paginate(10);

        return $this->fetch('', [
            'navTitle' => $this->navTitle,
            'commentList' => $commentList
        ]);
    }
}
